package org.scc.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.scc.model.User;

public class UserDao extends BaseDao{

	@Override
	protected String getMapper(String methodName) {
		return "org.scc.sqlmap.UserMapper." + methodName;
	}

	/**
	 * 新增用户
	 * @param user
	 */
	public void add(User user){
		insert(getMapper("add"), user, "add user error");
	}
	
	/**
	 * 更新用户
	 * @param user
	 */
	public void update(User user){
		update(getMapper("update"), user, "update user error");
	}
	
	/**
	 * 删除用户
	 * @param ids
	 */
	public void delete(String ids){
		delete(getMapper("delete"), ids, "delete users error");
	}
	
	/**
	 * 取得用户的所有数量
	 * @return
	 */
	public int getCount(){
		return selectOne(getMapper("getCount"), "get user count error");
	}
	
	/**
	 * 分页查询用户
	 * @param rowBeginIndex
	 * @param rowCount
	 * @return
	 */
	public List<User> findPage(int rowBeginIndex, int rowCount){
		Map<String, Integer> param = new HashMap<String, Integer>();
		param.put("rowBeginIndex", rowBeginIndex);
		param.put("rowCount", rowCount);
		return selectList(getMapper("findPage"), param, "find page user error");
	}
	
	/**
	 * 登陆
	 * @param username
	 * @param password
	 * @return
	 */
	public User login(String username, String password){
		Map<String, String> param = new HashMap<String, String>();
		param.put("username", username);
		param.put("password", password);
		return selectOne(getMapper("login"), param, "get user by username and password error");
	}
	
	/**
	 * 取得最大的用户名
	 * @return
	 */
	public String getMaxUsername(){
		return selectOne(getMapper("getMaxUserName"), "get max username error");
	}
	
	public User load(int id){
		return selectOne(getMapper("load"), id, "load user error");
	}
	
	public List<User> searchUser(String name){
		return selectList(getMapper("searchUser"), name, "search user error");
	}
	
	public List<User> findUsersIn(){
		return selectList(getMapper("findUsersIn"), "find user in error");
	}
	
	public List<User> findNoGroupUsers(){
		return selectList(getMapper("findNoGroupUsers"), "find user in error");
	}
}
